<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品规格管理</title>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.5.5.4/themes/metro/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.5.5.4/themes/icon.css"/>
    <script type="text/javascript" src="/static/jquery-easyui-1.5.5.4/jquery.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.5.5.4/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.5.5.4/locale/easyui-lang-zh_CN.js"></script>
</head>
<script type="text/javascript">
    var url;
    //添加
     function openCustomerAddDialog(){
         $("#dlg").dialog("open").dialog("setTitle", "添加商品规格");
    }

    function saveGoodsSpec(){
            url = "/admin/goodsSpec/save";
            $("#goodsSpecForm").form("submit", {
                url: url,
                onSubmit: function () {
                    return $(this).form("validate");
                },
                success: function (result) {
                    var result = eval('(' + result + ')');
                    if (result.success) {
                        $.messager.alert("系统提示", "保存成功！");
                        closeGoodsSpec();
                        $("#dg").datagrid("reload");
                        $("#goodsMold").combobox("reload");
                    } else {
                        $.messager.alert("系统提示", result.errorInfo);
                    }
                }
            });
    }

    function closeGoodsSpec(){
         $("#dlg").dialog("close");
    }

     //修改
     function openCustomerModifyDialog(){
       var selectdRows = $("#dg").datagrid("getSelections");
       if(selectdRows.length !=1){
            $.messager.alert("系统提示", "请选择一条要修改的数据");
            return;
       }
       var row = selectdRows[0];
       $("#dlg").dialog("open").dialog("setTitle", "修改商品规格");
       $("#goodsSpecForm").form("load", row);
       $("#spec_id").val(row.id);
       $("#attr_values").val(formatGoodsMoldName1(null,row));

     }

     //删除
      function deleteCustomer() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择要删除的数据");
                return;
            }

            var strIds = [];
            for (var i = 0; i < selectedRows.length; i++){
                strIds.push(selectedRows[i].id);
            }

            var ids = strIds.join(",");
             $.messager.confirm("系统提示", "您确定要删除这条数据吗?", function (r) {
                if (r) {
                    $.post("/admin/goodsSpec/delete", {ids: ids}, function (result) {
                        if (result.success) {
                            $.messager.alert("系统提示", "数据已成功删除！");
                            $("#dg").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    }, "json");
                }
            });
        }

    function formatGoodsMoldName(val, row) {
        return row.goodsMold?row.goodsMold.name:"";
    }

    function formatGoodsMoldName1(val, row) {
        console.log("row"+row);
        var a="";
        for (var i = 0;i<row.specItemList.length;i++){
            a+=row.specItemList[i].item+"\r\n";
        }
        return a;
    }

     function resetValue() {
         $("#goodsSpecForm").form("clear");
     }

</script>
<body style="margin: 1px">
<table id="dg" title="商品规格管理" class="easyui-datagrid"
       fitColumns="true" pagination="true" rownumbers="true"   singleSelect="true"
       url="/admin/goodsSpec/list" fit="true" method="get" toolbar="#goodsTypetools">
    <thead>
    <th field="db" checkbox="true" align="center"></th>
    <th field="id" width="20" align="center">编号</th>
    <th field="goodsMoldName" width="100" align="center" formatter="formatGoodsMoldName">规格类型</th>
    <th field="name" width="100" align="center">规格名称</th>
    <th field="strItem" width="100" align="center" formatter="formatGoodsMoldName1">规格项</th>
    </thead>
    <div id="goodsTypetools">
        <div>
            <a href="javascript:openCustomerAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
            <a href="javascript:openCustomerModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit"
               plain="true">修改</a>
            <a href="javascript:deleteCustomer()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
        </div>
        <div>
            &nbsp;类型名称：&nbsp;<input type="text" id="s_name" size="20" onkeydown="if(event.keyCode==13) searchCustomer()"/>
            <a href="javascript:searchCustomer()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
        </div>
    </div>
</table>

<div id="dlg" class="easyui-dialog" style="width: 800px;height: 500px;padding: 20px; border:none"
     closed="true" data-options="iconCls: 'icon-save',toolbar: '#dlg-toolbar',
				buttons: '#dlg-buttons',modal:true,onClose:function(){resetValue()}">
    <form id="goodsSpecForm" method="post">
        <table cellspacing="8px" style="width: 100%;">
            <input type="hidden" name="id" id="spec_id"/>
            <tr>
                <td>规格名称：</td>
                <td><input type="text" id="attr_name" name="name" class="easyui-validatebox" required="true"/> </td>
            </tr>
            <tr>
                <td>所属商品类型：</td>
                <td>
                    <input class="easyui-combobox easyui-validatebox" id="goodsMold" name="goodsMold" style="width:50%;" data-options="
                        url:'/admin/goodsmold/all',
                        method:'get',
                        valueField:'id',
                        textField:'name',
                        panelHeight:'auto'
					">
                </td>
            </tr>

            <tr>
                <td>规格项：</td>
                <td><textarea id="attr_values" name="strItem" class="easyui-validatebox" required="true" style="height:100px"></textarea> </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="saveGoodsSpec()">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#dlg').dialog('close')">关闭</a>
</div>
</body>
</html>